home *** CD-ROM | disk | FTP | other *** search
/ Risc World 3 / Risc World 3.iso / SOFTWARE / ISSUE6 / PD / PDF / pdf / c++ / Clip < prev    next >
Text File  |  2003-02-21  |  4KB  |  179 lines

  1. //--------------------------------------------------------------------------
  2. //
  3. //   Copyright (c) 2002, Colin Granville
  4. //
  5. //   All rights reserved.
  6. //
  7. //   Redistribution and use in source and binary forms, with or
  8. //   without modification, are permitted provided that the following 
  9. //   conditions are met:
  10. //
  11. //      * Redistributions of source code must retain the above copyright 
  12. //        notice, this list of conditions and the following disclaimer.
  13. //
  14. //      * Redistributions in binary form must reproduce the above 
  15. //        copyright notice, this list of conditions and the following 
  16. //        disclaimer in the documentation and/or other materials 
  17. //        provided with the distribution.
  18. //
  19. //      * The name Colin Granville may not be used to endorse or promote 
  20. //        products derived from this software without specific prior 
  21. //        written permission.
  22. //
  23. //   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
  24. //   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
  25. //   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
  26. //   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
  27. //   COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
  28. //   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
  29. //   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
  30. //   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
  31. //   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
  32. //   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
  33. //   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
  34. //   OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. //--------------------------------------------------------------------------
  37.  
  38. #include "Clip.h"
  39. #include "GuiBBox.h"
  40. #include "iostream.h"
  41.  
  42.  
  43. Clip::Clip()
  44.  : box(0)
  45. {
  46. }
  47.  
  48. //*******************************************************************
  49.  
  50. Clip::~Clip()
  51. {
  52.   clear();
  53. }
  54.  
  55. //*******************************************************************
  56.  
  57. void Clip::clear()
  58. {
  59.   while (box) doPop();
  60. }
  61.  
  62. //*******************************************************************
  63.  
  64. int Clip::init(const GuiBBox& b)
  65. {
  66.   clear();
  67.   box=new ClipBox;
  68.   if (box)
  69.   {
  70.     box->xmin=(b.xmin > b.xmax ? b.xmax : b.xmin);
  71.     box->xmax=(b.xmax > b.xmin ? b.xmax : b.xmin);
  72.     box->ymin=(b.ymin > b.ymax ? b.ymax : b.ymin);
  73.     box->ymax=(b.ymax > b.ymin ? b.ymax : b.ymin);
  74.     box->path=0;
  75.     box->removePath=0;
  76.     box->next=0;
  77.   }
  78.   return isOk();
  79. }
  80.  
  81. //*******************************************************************
  82.  
  83. void Clip::removePath()
  84. {
  85.   if (isOk() && box->removePath) 
  86.   {
  87.     delete [] box->path;
  88.     box->path=0;
  89.     box->removePath=0;
  90.   }
  91. }
  92.  
  93. //*******************************************************************
  94.  
  95. void Clip::push()
  96. {
  97.   if (!isOk()) return;
  98.   ClipBox* b=new ClipBox;
  99.   if (!b) return;
  100.  
  101.   *b=*box;
  102.   b->removePath=0; // don't delete path if it was copied
  103.   b->next=box;
  104.   box=b;
  105. }
  106.  
  107.  
  108. //*******************************************************************
  109.  
  110. void Clip::pop()
  111. {
  112.   if (isOk() && box->next) doPop();
  113. }
  114.  
  115. //*******************************************************************
  116.  
  117. void Clip::doPop()
  118. {
  119.   if (!isOk()) return;
  120.   ClipBox* b=box->next;
  121.   removePath();
  122.   delete box;
  123.   box=b;
  124. }
  125.  
  126. //*******************************************************************
  127. // pathArray[0]=size
  128. // pathArray[1]=draw type
  129. // pathArray ends with 0
  130. void Clip::setPath(int* pathArray)
  131. {
  132.   if (!pathArray) return;
  133.  
  134.   if (!isOk())
  135.   {
  136.     delete [] pathArray;
  137.     return;
  138.   }
  139.  
  140.   GuiBBox newbox;
  141.   Boundary_start(newbox);
  142.  
  143.   for (int* p=pathArray+2;*p!=0;p++)
  144.   {
  145.     switch (*p)
  146.     {
  147.       case 5:  break;
  148.       case 6:  Boundary_point(newbox,p[1],p[2]);
  149.                Boundary_point(newbox,p[3],p[4]);
  150.                Boundary_point(newbox,p[5],p[6]);
  151.                p+=6;
  152.                break;
  153.       default: Boundary_point(newbox,p[1],p[2]);
  154.                p+=2;
  155.                break;
  156.     }
  157.   }
  158.  
  159.   //get intersection of newbox and box
  160.   if (newbox.xmin>box->xmin) box->xmin=newbox.xmin;
  161.   if (newbox.xmax<box->xmax) box->xmax=newbox.xmax;
  162.   if (newbox.ymin>box->ymin) box->ymin=newbox.ymin;
  163.   if (newbox.ymax<box->ymax) box->ymax=newbox.ymax;
  164.  
  165.   if (box->removePath)
  166.   {
  167.     //can't clip to existing path so don't change it
  168.     // a bodge but it helps
  169.     delete [] pathArray;
  170.     return;
  171.   }
  172.  
  173.   removePath();
  174.   box->path=pathArray;
  175.   box->removePath=1;
  176. }
  177.  
  178.  
  179.